//
//  SexView.m
//  TravelFollowYourHeart
//
//  Created by appleLJ on 2017/12/12.
//  Copyright © 2017年 Lwx. All rights reserved.
//

#import "SexView.h"

@interface SexView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property(nonatomic,strong) UIView *backView;
@property(nonatomic,strong) UIView *lineView1;
@property(nonatomic,strong) UIView *lineView2;
@property(nonatomic,strong) UIButton *cancelButton;
@property(nonatomic,strong) UIButton *sureButton;
@property(nonatomic,strong) UIPickerView *pickerView;
@property(nonatomic,strong) NSArray *dataArray;

@property(nonatomic,strong) NSString *sexString;

@end

@implementation SexView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.alpha = 0;
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT)];
        bgView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75];
        self.sexString = self.dataArray[0];//设置默认值
        //添加控件
        [self addSubview:bgView];
        [self addSubview:self.backView];
        [self.backView addSubview:self.lineView1];
        [self.backView addSubview:self.lineView2];
        [self.backView addSubview:self.cancelButton];
        [self.backView addSubview:self.sureButton];
        [self.backView addSubview:self.pickerView];
        
        //约束
        __weak __typeof(self)weakSelf = self;
        [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(bgView);
            make.height.mas_equalTo(180 / WIDTH_5S_SCALE);
        }];
        [self.lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(weakSelf.backView);
            make.height.mas_equalTo(0.5);
        }];
        [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.lineView1.mas_bottom).offset(8 / WIDTH_5S_SCALE);
            make.left.equalTo(weakSelf.backView).offset(10 / WIDTH_5S_SCALE);
            make.width.mas_equalTo(56 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(30 / WIDTH_5S_SCALE);
        }];
        [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(weakSelf.cancelButton);
            make.centerY.equalTo(weakSelf.cancelButton);
            make.right.equalTo(weakSelf.backView).offset(-10 / WIDTH_5S_SCALE);
        }];
        [self.lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf.backView);
            make.height.equalTo(weakSelf.lineView1);
            make.top.equalTo(weakSelf.cancelButton.mas_bottom).offset(8 / WIDTH_5S_SCALE);
        }];
        [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.lineView2.mas_bottom);
            make.left.bottom.right.equalTo(weakSelf.backView);
        }];
    }
    return self;
}

//出现
- (void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

//消失
- (void)dismiss{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

//确定
- (void)sureBtnEvent{
    NSLog(@"确定");
    if (self.getSexHanle) {
        self.getSexHanle(self.sexString);
        [self dismiss];
    }
}

#pragma makr -- UIPickerViewDelegate & UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.dataArray.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return SCREEN_WIDTH;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 30 / WIDTH_5S_SCALE;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component{
    self.sexString = self.dataArray[row];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    //设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor = getColor(dividerColor);
        }
    }
    //设置文字的属性
    UILabel *genderLabel = [[UILabel alloc] init];
    genderLabel.font = DEF_FontSize_16;
    genderLabel.textAlignment = NSTextAlignmentCenter;
    genderLabel.textColor = getColor(greenBGColor);
    genderLabel.text = self.dataArray[row];
    return genderLabel;
}

#pragma mark -- init

- (UIView *)backView{
    if (!_backView) {
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = getColor(whiteColor);
    }
    return _backView;
}

- (UIView *)lineView1{
    if (!_lineView1) {
        _lineView1 = [[UIView alloc] init];
        _lineView1.backgroundColor = getColor(dividerColor);
    }
    return _lineView1;
}

- (UIView *)lineView2{
    if (!_lineView2) {
        _lineView2 = [[UIView alloc] init];
        _lineView2.backgroundColor = getColor(dividerColor);
    }
    return _lineView2;
}

- (UIButton *)cancelButton{
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc] init];
        _cancelButton.titleLabel.font = DEF_FontSize_14;
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:getColor(greenBGColor) forState:UIControlStateNormal];
        [_cancelButton setBackgroundColor:getColor(@"F6F6F6")];
        _cancelButton.layer.borderWidth = 1;
        _cancelButton.layer.borderColor = getColor(@"C6C6C6").CGColor;
        _cancelButton.layer.cornerRadius = 5;
        _cancelButton.layer.masksToBounds = YES;
        [_cancelButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}

- (UIButton *)sureButton{
    if (!_sureButton) {
        _sureButton = [[UIButton alloc] init];
        _sureButton.titleLabel.font = DEF_FontSize_14;
        [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
        [_sureButton setTitleColor:getColor(whiteColor) forState:UIControlStateNormal];
        [_sureButton setBackgroundColor:getColor(mainColor)];
        _sureButton.layer.cornerRadius = 5;
        _sureButton.layer.masksToBounds = YES;
        [_sureButton addTarget:self action:@selector(sureBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureButton;
}

- (UIPickerView *)pickerView{
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.backgroundColor = getColor(whiteColor);
        _pickerView.showsSelectionIndicator = YES;
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
    }
    return _pickerView;
}

- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[@"男",@"女",@"未知"];
    }
    return _dataArray;
}

@end
